#include "qlineanalyzer.h"

QLineAnalyzer* QLineAnalyzer::m_instance = NULL;

QLineAnalyzer::QLineAnalyzer()
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
}

QLineAnalyzer::~QLineAnalyzer()
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
}

QLineAnalyzer* QLineAnalyzer::instance()
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	if(NULL == m_instance)
	{
		m_instance = new QLineAnalyzer();
	}
	return m_instance;
}

QBDDItem* QLineAnalyzer::analyze(QStringList header,QStringList value)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
	if(header.size() == value.size())
	{
		QBDDItem* item;

		item = match<QSelfItem>(header,value);
		if(NULL != item)
			return item;

		
		item = match<QResistorItem>(header,value);
		if(NULL != item)
			return item;

		
		item = match<QCapacitorItem>(header,value);
		if(NULL != item)
			return item;

		
		item = match<QLedItem>(header,value);
		if(NULL != item)
			return item;

		item = new QBDDItem(header,value);
		return item;
	}
	return NULL;
}


template <class T>
T* QLineAnalyzer::match(QStringList header,QStringList values)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
	if(header.contains(LA_TYPE_1) && header.contains(LA_TYPE_2))
	{
		quint8 indexCat_1 = header.indexOf(LA_TYPE_1);
		quint8 indexCat_2 = header.indexOf(LA_TYPE_2);

		foreach(QString cat1,T::cat_1())
		{
			foreach(QString cat2,T::cat_2())
			{
				if(values[indexCat_1] == cat1 && values[indexCat_2] == cat2)
				{
					return (new T(header,values));
				}
				else
					return NULL;
			}
		}
	}
	return NULL;
}